#include "list.h"

//题目来源:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
/**
 * @brief 删除有序链表中的重复结点
 *
 * @param head
 * @return ListNode*
 */
ListNode *deleteDuplicates(ListNode *head) {
  if (head == nullptr) {
    return;
  }
  ListNode *pre = head;
  ListNode *currNode = head->next_;

  while (currNode && pre) {
    if (pre->data_ == currNode->data_) {
      currNode->next_ = pre->next_;
      delete pre;
      pre = currNode->next_;
      continue;
    }
    currNode = currNode->next_;
    pre = pre->next_;
  }

  return head;
}

ListNode *deleteDuplicates(ListNode *head) {
  if (head == nullptr) {
    return nullptr;
  }

  ListNode *pre = head;
  while (pre && pre->next_) {
    if (pre->data_ == pre->next_->data_) {
      ListNode *temp = pre->next_;
      pre->next_ = pre->next_->next_;
      delete temp;
      continue;
    }
    pre = pre->next_;
  }

  return head;
}